package cn.boy.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.boy.service.AdminstratorService;
import cn.boy.service.AdminstratorServiceFactory;
import cn.boy.service.StudentService;
import cn.boy.service.StudentServiceFactory;
import cn.boy.utils.Util;

public class UpdateStudentPasswordServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		
		String no = request.getParameter("no");
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		
		StudentServiceFactory instance = StudentServiceFactory.getInstance();
		StudentService service = instance.getStudentService();
		String retValue = service.login(no, oldPassword);
		
		if(!Util.isEmpty(retValue)){
			service.updateStudent(no, newPassword);
			response.getOutputStream().write("success".getBytes("UTF-8"));
		}else{//old password is not right
			response.getOutputStream().write("wrong".getBytes("UTF-8"));
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
